<?php
/**
 * 品牌管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('BrandModel', 'product');
class BrandController extends UE_Controller_Admin {
	public function listAction() {
		BrandModel::getBrands();
		$filter = $this->id == 'trash' ? ' brand_is_trash = 1' : ' brand_is_trash != 1';
		$db = UE_DB::factory();
		$totals = $db->getfirstField("SELECT COUNT(*) FROM ue_brand WHERE $filter");
		$brands = array();
		$pagingStr = '';
		if ($totals) {
			$paging = new UE_Paging();
			$paging->setVar($totals, 15);
			$sql = "SELECT * FROM ue_brand WHERE $filter ORDER BY brand_order ASC LIMIT {$paging->firstCount}, {$paging->pageNo}";
			$brands = UE_DB::factory()->getAll($sql);
			$pagingStr = $paging->pagingStr;
		}
		
	    include $this->tpl();
	}
	
	public function addAction() {
		$cats = CatModel::getCats('product', 0, false);
		//print_r($cats);
		if (IS_POST) {
			$brandModel = new BrandModel();
			if ($brandModel->add($_POST)) {
				$this->setOK('成功添加品牌');
			} else {
				$this->setErr($brandModel->getErrs());
			}
		}
		$ueid = makeUeid();
	    include $this->tpl();
	}
	
	public function enableAction() {
		$this->id = (int) $this->id;
	    if (!$this->id) {
	    	print 'Error Param!';
	    	return ;
	    }
	    
	    $brandModel = new BrandModel();
	    $brandModel->setId($this->id)->enable();
	    header('Location: '.UE::referer());
	}
	
	public function disableAction() {
		$this->id = (int) $this->id;
	    if (!$this->id) {
	    	print 'Error Param!';
	    	return ;
	    }
	    
	    $brandModel = new BrandModel();
	    $brandModel->setId($this->id)->disable();
	    header('Location: '.UE::referer());
	}
	
	public function editAction() {
		$this->id = (int) $this->id;
	    if (!$this->id) {
	    	print 'Error Param!';
	    	return ;
	    }
		
	    $brandModel = new BrandModel();
	    $brandModel->setId($this->id);
	    
	    if (IS_POST) {
	    	if ($brandModel->update($_POST)) {
	    		$this->setOK('成功编辑品牌');
			} else {
				$this->setErr($brandModel->getErrs());
			}
	    }
	    
	    $brandItem = $brandModel->read();
	    
	    
	    $brandItem['cats'] = array();
	    
	    if ($brandItem) {
	    	$rs = UE_DB::factory()->getAll("SELECT cat_id FROM ue_brand_r_cat WHERE brand_id='{$this->id}'");
	    	if ($rs) {
	    		foreach ($rs as $r) {
	    			$brandItem['cats'][] = $r['cat_id'];
	    		}
	    		
	    	}
	    }
	    
	    //print_r($brandItem);exit;
	    
		$cats = CatModel::getCats('product', 0, false);
	    include $this->tpl();
	}
	
	public function delAction() {	    
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		 
		$brandModel = new BrandModel();
	    $brandModel->setId($this->id);
		
	    if(!$brandModel->delete()) {
	    	$this->setErr($brandModel->getErrs());
	    	$this->showMessage();
			return false;
	    }
	    
	    header('Location: '.UE::referer());
	}
	
	public function trashAction() {	    
		if (empty($this->id)) {
			print "Error Param!";
			return false;
		}
		
		$isTrash = empty($_GET['untrash']) ? 1 : 0;
		
		$db = UE_DB::factory();
	    if(!$db->query("UPDATE ue_brand SET brand_is_trash = '{$isTrash}' WHERE brand_id = '{$this->id}'")) {
	    	$this->setErr($db->getLastErr());
	    	$this->showMessage();
			return false;
	    }
	    
	    header('Location: '.UE::referer());
	}
	
}